package com.bjqh.edu.UI.Activity.Index;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.Index;
import com.bjqh.edu.Bean.OpenClass;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.dialog.LoadDialog;
import com.bjqh.edu.adapter.OpenClassAdapter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGABanner;
import okhttp3.Call;
import okhttp3.Response;

public class OpenClassActivity extends AppCompatActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_qx)
    TextView tvQx;
    @BindView(R.id.lv)
    ListView lv;
    @BindView(R.id.rl_top_tilte)
    RelativeLayout rlTopTilte;
    @BindView(R.id.op_title)
    TextView opTitle;
    @BindView(R.id.banner)
    BGABanner banner;
    @BindView(R.id.sv)
    ScrollView sv;

    private List<OpenClass.RowsBean> rowsBeen;
    private List<OpenClass.BannerBean> bannerBeen;
    OpenClassAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_open_class);
        ButterKnife.bind(this);
        int height = getResources().getDisplayMetrics().heightPixels;
        final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) banner.getLayoutParams();
        //获取当前控件的布局对象
        params.height=height/4;//设置当前控件布局的高度
        banner.setLayoutParams(params);//将设置好的布局参数应用到控件中
        LoadDialog.initDialog(this, "火速加载中...");
        tvTitle.setText("公开课");
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        banner.setAutoPlayAble(true);
        OkGo.post(AppConstants.BASE_URL + "/pubCourse/datalist.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.i("公开课", s);
                        Gson gson = new Gson();
                        OpenClass openClass = gson.fromJson(s, OpenClass.class);
                        if (openClass.getMsg().equals("success")) {
                            LoadDialog.closeDialog();
                            opTitle.setText(openClass.getTitle());
                            if (openClass.getRows() != null) {
                                rowsBeen = openClass.getRows();
                                adapter = new OpenClassAdapter(OpenClassActivity.this, rowsBeen);
                                lv.setAdapter(adapter);
                                AppConstants.setListViewHeightBasedOnChildren(lv);
                                sv.smoothScrollTo(0, 0);
                                bannerBeen = openClass.getBanner();
                                banner.setData(R.layout.item_banner2, bannerBeen, null);
                                banner.setAdapter(new BGABanner.Adapter<RelativeLayout, OpenClass.BannerBean>() {
                                    @Override
                                    public void fillBannerItem(BGABanner banner, RelativeLayout itemView, OpenClass.BannerBean model, int position) {
                                        ImageView img = (ImageView) itemView.findViewById(R.id.img);
                                        TextView content = (TextView) itemView.findViewById(R.id.content);
                                        TextView title = (TextView) itemView.findViewById(R.id.title);
                                        Glide.with(OpenClassActivity.this).load(AppConstants.IMG_URL + model.getImg()).diskCacheStrategy(DiskCacheStrategy.ALL
                                        ).error(R.mipmap.ls).into(img);
                                        String[] coArray = model.getName().split(",");
                                        title.setText(coArray[0]);
                                        if (coArray.length >= 2)
                                            content.setText(coArray[1]);
                                    }
                                });
                            }
                        }
                    }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Toast.makeText(OpenClassActivity.this, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                        LoadDialog.closeDialog();
                    }
                });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (rowsBeen.size() > 0 && rowsBeen != null) {
                    Integer aaa = rowsBeen.get(i).getVid();
                    if (aaa != null) {
                        Intent intent = new Intent(OpenClassActivity.this, Video2Activity.class);
                        intent.putExtra("rid", rowsBeen.get(i).getVid());
                        // intent.putExtra("name", dataBeen.get(i).getName());
                        startActivity(intent);

                    }

                }
            }
        });
        // sv.smoothScrollBy(0,20);
    }

}
